import json
import pandas as pd
import os
import traceback

name = ['Title', 'Authors', 'Source', 'Indexed', 'Keywords', 'Address', 'Emails', 'DocTypes', 'CategoryInfo',
        'CorrespondingAuthor']
result = pd.DataFrame(columns=name)
{}.keys()


def addRow(i, data):
    data = data['1']
    if 'en' in data['titles']['item']:
        title = data['titles']['item']['en'][0]['title']
    else:
        lang = data['titles']['item'].keys().__iter__().__next__()
        title = data['titles']['item'][lang][0]['title']
    authors = ""
    emails = ""
    if 'en' in data['names']['author']:
        authorList = data['names']['author']['en']
    else:
        lang = data['names']['author'].keys().__iter__().__next__()
        authorList = data['names']['author'][lang]

    for author in authorList:
        if author.get('email_addr', False):
            emails += "{};".format(author['email_addr'])
        authors += "{seq_no}:{name}".format(seq_no=author['seq_no'], name=author['full_name'])
        if author.get("addr_no", False):
            authors += "[{}]".format(author["addr_no"])
        authors += ';'

    if 'en' in data['titles']['source']:
        source = data['titles']['source']['en'][0]['title']
    else:
        lang = data['titles']['source'].keys().__iter__().__next__()
        source = data['titles']['source'][lang][0]['title']

    indexed = data['dates']['create_date']
    keywords = ""
    if data.get('keywords', False):
        keywords += "keywords:{};".format(";".join(data['keywords']))
    if data['item'].get('keywords_plus', False):
        keywords += "keywords_plus:{};".format(";".join(data['item']['keywords_plus']))

    authorInfo = ""
    for address in data['addresses']:
        address = address[0]
        authorInfo += "[{addr_no}]{full_address};".format(addr_no=address['addr_no'],
                                                          full_address=address['full_address'])
    reprintAuthor = ""
    if data.get('reprint_addresses', False):
        for c_authors in data['reprint_addresses']:
            c_authors = c_authors[1]
            for author in c_authors:
                reprintAuthor += "{seq_no}:{name}".format(seq_no=author['seq_no'], name=author['full_name'])
                if author.get("addr_no", False):
                    reprintAuthor += "[{}]".format(author["addr_no"])
                reprintAuthor += ';'

    docTypes = ""
    if data.get('doctypes', False):
        docTypes = ",".join(data['doctypes'])

    categoryInfo = ""
    if data.get('category_info', False):
        for cate in data['category_info']['subjects']:
            if cate['ascatype'] == "extended":
                categoryInfo += cate['subject']

    result.loc[i] = [title, authors, source, indexed, keywords, authorInfo, emails, docTypes, categoryInfo,
                     reprintAuthor]


if __name__ == '__main__':

    g = os.walk("./result")
    i = 0
    for path, dir_list, file_list in g:
        for file_name in file_list:
            if file_name.endswith(".json"):
                with open(
                        os.path.join(path, file_name),
                        'r') as f:
                    article = json.loads(f.read())
                try:
                    addRow(i, article)
                    i += 1
                except Exception as e:
                    colluid = article['1']['colluid']
                    with open("./error/{}.json".format(colluid), 'w') as f:
                        f.write(json.dumps(article))
                    traceback.print_exc()
                    continue
    result.to_excel('result.xlsx')
